﻿# ------------------------------- #
# DNS Server Record Configuration #
# ------------------------------- #
param([string]$virtualMachineService, [string]$virtualMachineName)

# Return host IP
function Get-HostIP($hostType, $host)
{
	switch ($hostType)
	{
		"Public"
		{
			return (Get-AzureVM -ServiceName ($subscription.virtualMachines.virtualMachine | ? {$_.name -eq $host}).service -Name $host | Get-AzureEndpoint)[0].Vip
		}
		"Private"
		{
			return (Get-AzureVM -ServiceName ($subscription.virtualMachines.virtualMachine | ? {$_.name -eq $host}).service -Name $host).IpAddress
		}
		"Static"
		{
			return $host
		}
		default
		{
			throw "DNS record host type '$hostType' is not supported."
		}
	}
}

Write-Information "Virtual machine '$virtualMachineName' DNS record configuration ... " -NoNewLine $true
$userName = $subscription.virtualMachines.adminUserName
$password = $subscription.virtualMachines.password
$netBiosDomain = $subscription.virtualMachines.netBiosDomain
$uri = Get-AzureWinRMUri -ServiceName $virtualMachineService -Name $virtualMachineName
$credential = New-Object System.Management.Automation.PSCredential(($netBiosDomain + "\" + $userName), (ConvertTo-SecureString $password -AsPlainText -Force))
$session = New-PSSession -ComputerName $uri[0].DnsSafeHost -Credential $credential -Port (Get-VirtualMachineInstanceEndpointPort $subscription $virtualMachineName) -UseSSL
foreach ($record in $script.records.record)
{
	switch ($record.type)
	{
		"A"
		{
			Invoke-Command -Session $session -ScriptBlock {
				param([string]$zone, [string]$name, [string]$address)
				if ((Get-DnsServerResourceRecord -ZoneName $zone | where {$_.RecordType -eq "A" -and $_.HostName -eq $name}) -ne $null)
				{
					Remove-DnsServerResourceRecord -ZoneName $zone -Name $name -RRType A -Force
				}				
				Add-DnsServerResourceRecordA -ZoneName $zone -Name $name -IPv4Address $address
			} -ArgumentList $record.zone, $record.name, (Get-HostIP $record.hostType $record.host)
		}
		"CNAME"
		{
			Invoke-Command -Session $session -ScriptBlock {
				param([string]$zone, [string]$name, [string]$alias)
				if ((Get-DnsServerResourceRecord -ZoneName $zone | where {$_.RecordType -eq "CNAME" -and $_.HostName -eq $name}) -ne $null)
				{
					Remove-DnsServerResourceRecord -ZoneName $zone -Name $name -RRType CNAME -Force
				}				
				Add-DnsServerResourceRecordCName -ZoneName $zone -Name $name -HostNameAlias $alias
			} -ArgumentList $record.zone, $record.name, $record.alias
		}
		"MX"
		{
			Invoke-Command -Session $session -ScriptBlock {
				param([string]$zone, [string]$name, [string]$mailExchange, [string]$priority)
				if ((Get-DnsServerResourceRecord -ZoneName $zone | where {$_.RecordType -eq "MX" -and $_.HostName -eq $name}) -ne $null)
				{
					Remove-DnsServerResourceRecord -ZoneName $zone -Name $name -RRType MX -Force
				}				
				Add-DnsServerResourceRecordMX -ZoneName $zone -Name $name -MailExchange $mailExchange -Preference $priority
			} -ArgumentList $record.zone, $record.name, $record.mailExchange, $record.priority
		}
		default
		{
			throw "DNS record '$($record.type)' is not supported."
		}
	}
}
Remove-PSSession $session
Write-Text "done."